package com.wensi.helper

import android.content.Context
import android.media.SoundPool

/**
 * 播放铃声工具
 */
object SoundHelper {
    private lateinit var context: Context
    private lateinit var soundPool: SoundPool

    fun initSound(context: Context) {
        this.context = context
        soundPool = SoundPool.Builder().setMaxStreams(1).build()
    }

    /**
     * 短声音播放
     */
    fun playVoice(resId: Int) {
        val poolId = soundPool.load(context, resId, 1)
        soundPool.setOnLoadCompleteListener { soundPool, _, status ->
            if (status == 0) {
                /**
                 * 参数1  Id
                 * 参数2  左音量0.0f - 1.0f
                 * 参数3  右音量0.0f - 1.0f
                 * 参数4  优先级
                 * 参数5  重复数
                 * 参数6  速率 0.5f - 2.0f
                 */
                soundPool.play(poolId, 1f, 1f, 1, 0, 1f)
            }
        }
    }
}